package com.koron.order.repair.service.impl;

import com.koron.common.core.util.CodeTools;
import com.koron.order.repair.bean.entity.RepairReadyPo;
import com.koron.order.repair.mapper.RepairReadyMapper;
import com.koron.order.repair.service.RepairReadyService;
import org.apache.commons.collections4.CollectionUtils;
import org.koron.ebs.mybatis.SessionFactory;
import org.koron.ebs.mybatis.TaskAnnotation;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * (RepairReady)表服务实现类
 *
 * @author zhouj
 * @since 2021-09-14 14:12:53
 */
@Service("repairReadyService")
public class RepairReadyServiceImpl implements RepairReadyService {

    @Override
    @TaskAnnotation("insert")
    public int insert(SessionFactory factory, RepairReadyPo repairReady) {

        RepairReadyMapper mapper = factory.getMapper(RepairReadyMapper.class);
        repairReady.setId(CodeTools.getCode32());

        return mapper.insert(repairReady);
    }

    @Override
    @TaskAnnotation("update")
    public int update(SessionFactory factory, RepairReadyPo repairReady) {
        RepairReadyMapper mapper = factory.getMapper(RepairReadyMapper.class);
        return mapper.update(repairReady);
    }

    @Override
    @TaskAnnotation("deleteById")
    public int deleteById(SessionFactory factory, String id) {

        RepairReadyMapper mapper = factory.getMapper(RepairReadyMapper.class);
        return mapper.deleteById(id);
    }

    @Override
    @TaskAnnotation("deleteByOrderId")
    public int deleteByOrderId(SessionFactory factory, String orderId) {

        RepairReadyMapper mapper = factory.getMapper(RepairReadyMapper.class);
        return mapper.deleteByOrderId(orderId);
    }

    @Override
    @TaskAnnotation("queryById")
    public RepairReadyPo queryById(SessionFactory factory, String id) {

        RepairReadyMapper mapper = factory.getMapper(RepairReadyMapper.class);

        return mapper.queryById(id);
    }

    @Override
    @TaskAnnotation("queryList")
    public List<RepairReadyPo> queryList(SessionFactory factory, RepairReadyPo repairReady) {

        RepairReadyMapper mapper = factory.getMapper(RepairReadyMapper.class);

        List<RepairReadyPo> list = mapper.queryList(repairReady);

        return list;
    }

    @Override
    @TaskAnnotation("queryByOrderId")
    public List<RepairReadyPo> queryByOrderId(SessionFactory factory, String orderId) {

        RepairReadyMapper mapper = factory.getMapper(RepairReadyMapper.class);

        List<RepairReadyPo> list = mapper.queryByOrderId(orderId);

        return list;
    }

    @Override
    @TaskAnnotation("insertBatch")
    public int insertBatch(SessionFactory factory, List<RepairReadyPo> repairReadyPoList) {

        if (CollectionUtils.isEmpty(repairReadyPoList)) {
            return 0;
        }
        String repairId = repairReadyPoList.get(0).getRepairId();
        deleteByOrderId(factory, repairId);

        RepairReadyMapper mapper = factory.getMapper(RepairReadyMapper.class);
        repairReadyPoList.forEach(p -> {
            p.setId(CodeTools.getCode32());
        });

        return mapper.insertBatch(repairReadyPoList);
    }

    @Override
    @TaskAnnotation("updateBatch")
    public int updateBatch(SessionFactory factory, List<RepairReadyPo> repairReadyPoList) {

        if (CollectionUtils.isEmpty(repairReadyPoList)) {
            return 0;
        }

        RepairReadyMapper mapper = factory.getMapper(RepairReadyMapper.class);

        return mapper.updateBatch(repairReadyPoList);
    }

}